/*
 * Task 3. Detecting anagrams
 */
import java.util.*;
import java.io.*;

/**
 * Class for detecting if two phrases are anagrams
 * 
 * @author Vlad Oblamskyi
 */
public class AnagramDetector {
    private static final String WHITESPACE = "\\s";
    
    /*
     * Preparing string for further operations
     */
    private static String prepareString(String string) {
        return string.replaceAll(WHITESPACE, "").toLowerCase();
    }

    public static boolean areAnagrams(String phraseOne, String phraseTwo) {
        /*
         * Dividing phrases into code points arrays and sorting them. If
         * arrays are equal then phrases are anagrams.
         */
        int[] phraseOneCodes = getCodePoints(prepareString(phraseOne));
        int[] phraseTwoCodes = getCodePoints(prepareString(phraseTwo));

        Arrays.sort(phraseOneCodes);
        Arrays.sort(phraseTwoCodes);

        return Arrays.equals(phraseOneCodes, phraseTwoCodes);
    }
    
    private static int[] getCodePoints(String string) {
        int codePointsNumber = string.codePointCount(0, string.length());
        int[] codePoints = new int[codePointsNumber];
        
        for (int offset = 0; offset < codePoints.length; offset++) {
            int pointIndex = string.offsetByCodePoints(0, offset);
            codePoints[offset] = string.codePointAt(pointIndex);
        }
        
        return codePoints;
    }
    
    private static String getPhraseInput(Scanner in) {
        System.out.print("Enter phrase: ");
        return in.nextLine();
    }
    
    private static void printResult(String result) {
        System.out.println("Result: " + result);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        String phraseOne = getPhraseInput(in); 
        String phraseTwo = getPhraseInput(in); 
        
        boolean result = areAnagrams(phraseOne, phraseTwo);        
        
        printResult("anagrams status - " + result);
        
        in.close();
    }
}